 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spFdsCNCreatePartialInternalPerson')
	BEGIN
		PRINT 'Dropping Procedure spFdsCNCreatePartialInternalPerson'
		DROP  Procedure  spFdsCNCreatePartialInternalPerson    
	END

GO

PRINT 'Creating Procedure spFdsCNCreatePartialInternalPerson'
GO
/******************************************************************************
**		VSS File: $/SPAddNewUser.sql 
**		Name: spFdsCNCreatePartialInternalPerson    
**		Desc: 	This procedure adds a new user from Active Directory into the CDS.
**              
**		Return values: None
** 
**		Called by:   
**              AD_To_CDS.dtsx
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE Procedure spFdsCNCreatePartialInternalPerson (
	/* Param List */
	@firstName	varchar(50),
	@lastName	varchar(50),
	@employeeNumber varchar(12),
	@primOrgId		int = 1,
	@defPrincipalTypeId int = 1,
	@defOrgId		int = 1,
	@defFacilityId	int = 1
)	   


AS    
    ------------------------
    -- CONSTANTS
    DECLARE @DEFAULT_APP_BITMASK  AS INT
	DECLARE @INTERNAL_PERSON_TYPE AS INT
	    
    SET @DEFAULT_APP_BITMASK = 0
    SET @INTERNAL_PERSON_TYPE = 1
    
    ------------------------
    
    -- Placeholders for IDs passed back and 
	-- current max IDs of tables to be inserted
	-- into, since there are no Identities
    DECLARE
	        @_errorCode					AS INT,
			@_maxPrincipalId			AS INT,
			@_maxAdmPersonId			AS INT,
			@_maxAdmInternalPersonId	AS INT

	SET @_errorCode  = 0

	-- Setup the IDs to be inserted by selecting the max
	-- Id of the column, and if null, start at 1.
	SET @_maxPrincipalId = COALESCE (((SELECT MAX(Id) from FwkDomainPrincipal)+1), 1)

	-- AdmPerson
	SET @_maxAdmPersonId = COALESCE (((SELECT MAX(Id) from AdmPerson)+1), 1)

	-- AdmInternalPerson	
	SET @_maxAdmInternalPersonId = COALESCE (((SELECT MAX(Id) from AdmInternalPerson)+1), 1)


	-- START THE TRANS!	    
        BEGIN TRANSACTION spFdsCNCreatePartialIP 
        
        SET IDENTITY_INSERT FwkDomainPrincipal ON
           
        -- DmnPrincipal Insert 
	    INSERT INTO FwkDomainPrincipal ( Id,	
									FwkDomainPrincipalTypeID, 
									IsActive )
				VALUES 
							(	@_maxPrincipalId,
								@defPrincipalTypeId, 
								1 )           
								
        SET @_errorCode = @@ERROR
        
        IF @_errorCode <> 0 BEGIN
            ROLLBACK TRANSACTION spFdsCNCreatePartialIP
            RAISERROR('Unable to create FwkDomainPrincipal record for employee number %s', 16, 1, @employeeNumber)
            RETURN @_errorCode
        END
        
        ELSE BEGIN
            -- Check the Organization ID passed in isn't null, if it is, use the default.
            IF @primOrgId IS NULL 
				SET @primOrgId = @defOrgId
            END

			SET IDENTITY_INSERT FwkDomainPrincipal OFF
			SET IDENTITY_INSERT AdmPerson ON
          
            -- DmnPerson Insert
            INSERT INTO AdmPerson (	Id,
									FirstName, 
									LastName, 
									AdmPersonTypeId)
									
                VALUES			(	@_maxAdmPersonId,
									@firstName, 
									@lastName, 
									@INTERNAL_PERSON_TYPE )
									
            SET @_errorCode = @@ERROR
			SET IDENTITY_INSERT AdmPerson OFF
        
            IF @_errorCode <> 0 BEGIN
                ROLLBACK TRANSACTION spFdsCNCreatePartialIP
                RAISERROR('Unable to create AdmPerson record for employee ID %s', 16, 1, @employeeNumber)
                RETURN @_errorCode
            END
            ELSE BEGIN
				-- AdmInternalPerson Insert.
				SET IDENTITY_INSERT AdmInternalPerson ON
				INSERT INTO AdmInternalPerson ( Id,
												AdmPersonId,
												PrimaryFacilityId,
												PrimaryOrganizationId, 
												EmployeeNumber ) 
												VALUES
											  ( @_maxAdmInternalPersonId,
												@_maxAdmPersonId,
												@defFacilityId,
												@primOrgId, 
												@employeeNumber )

				SET @_errorCode = @@ERROR
                IF @_errorCode <> 0 BEGIN
					ROLLBACK TRANSACTION spFdsCNCreatePartialIP
					RAISERROR('Unable to create AdmInternalPerson record for employee Id %s', 16, 1, @employeeNumber)
					RETURN @_errorCode
				END
 				SET IDENTITY_INSERT AdmInternalPerson OFF
               			
				COMMIT TRANSACTION spFdsCNCreatePartialIP
       
    END


GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
